package com.download.zhuaba.Controller;

import com.download.zhuaba.Dao.UserRepository;
import com.download.zhuaba.Entity.JsonResult;
import com.download.zhuaba.Entity.User;
import com.download.zhuaba.Service.MailService;
import com.download.zhuaba.Utils.enCodeByBase64;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.*;

@Controller
public class LoginController {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private MailService mailService;

    @GetMapping("/")
    public String login(HttpSession session) {
        session.setAttribute("login", "1");
        session.setAttribute("flag", "1");
        return "login";
    }

    @RequestMapping(value = "/loginController", method = RequestMethod.POST)
    public String loginAdmin(@RequestParam("EmailOrUsername") String EmailOrUsername, @RequestParam("Password") String Password, RedirectAttributes attr, HttpSession session) throws Exception {
        List<User> list = new ArrayList<User>();
        list = userRepository.findAll();
        enCodeByBase64 code=new enCodeByBase64();
        if (list != null) {
            for (User user1 : list) {
                if (user1.getEmail().equals(EmailOrUsername) || user1.getUsername().equals(EmailOrUsername)) {
                    if (code.deCode(user1.getPassword()).equals(Password)) {
                        session.setAttribute("loginUser", user1.getId());
                        return "redirect:http://localhost:8080/welcome.html";
                    }
                }
            }
        }
        session.setAttribute("flag", "0");//跳转地址不带上u2参数
        session.setAttribute("login", "1");
        return "login";
    }
    @ResponseBody
    @RequestMapping(value = "/getCode", method = RequestMethod.POST)
    public JsonResult<Map<String, Integer>> getCode(@RequestParam("Email") String Email, @RequestParam("Password") String Password, RedirectAttributes attr, HttpSession session) {
        JsonResult<Map<String, Integer>> result1 = new JsonResult<>();
        String result=mailService.sendSimpleMailMessage(Email,"忘记密码");
        session.setAttribute("code", result);
        return result1;
    }

    @ResponseBody
    @RequestMapping(value = "/login/getLogin", method = RequestMethod.POST)
    public JsonResult<Map<String, Integer>> getUserAmount(HttpServletRequest request) {
        JsonResult<Map<String, Integer>> result = new JsonResult<>();
        try {
            Map<String, Integer> amountMap = new HashMap<>();
            if (request.getSession().getAttribute("login") == "0") {
                amountMap.put("login1", 0);
            } else {
                amountMap.put("login1", 1);
            }
            if (request.getSession().getAttribute("flag") == "0") {
                amountMap.put("flag1", 0);
            } else {
                amountMap.put("flag1", 1);
            }
            if (request.getSession().getAttribute("forget") == "0") {
                amountMap.put("forget1", 0);
            } else if(request.getSession().getAttribute("forget") == "1"){
                amountMap.put("forget1", 1);
            }
            else {
                amountMap.put("forget1", 2);
            }
            result.setData(amountMap);
            //这里举个例子，如果觉得setData穿的信息不够，还可以用setMessage方法多传一个字符串过去
            result.setMessage("login");

        } catch (Exception e) {
        }
        return result;
    }
    @ResponseBody
    @RequestMapping(value = "/forgetPassword", method = RequestMethod.POST)
    public JsonResult<Map<String, Integer>> getForgetPassword(@RequestParam("Code") String Code,@RequestParam("Email") String Email, @RequestParam("Username") String Username, @RequestParam("Password") String Password, HttpServletRequest request, HttpSession session) throws Exception {
        List<User> list = new ArrayList<User>();
        list = userRepository.findAll();
        enCodeByBase64 code=new enCodeByBase64();
        if (list != null) {
            for (User user1 : list) {
                if (user1.getEmail().equals(Email) && user1.getUsername().equals(Username)) {
                    if(!request.getSession().getAttribute("code").equals(Code)){
                        session.setAttribute("forget","3");
                        session.setAttribute("login", "1");
                        session.setAttribute("flag", "1");
                        JsonResult<Map<String, Integer>> result = new JsonResult<>();
                        try {
                            Map<String, Integer> amountMap = new HashMap<>();
                            if (request.getSession().getAttribute("login") == "0") {
                                amountMap.put("login1", 0);
                            } else {
                                amountMap.put("login1", 1);
                            }
                            if (request.getSession().getAttribute("flag") == "0") {
                                amountMap.put("flag1", 0);
                            } else {
                                amountMap.put("flag1", 1);
                            }
                            if (request.getSession().getAttribute("forget") == "0") {
                                amountMap.put("forget1", 0);
                            } else if(request.getSession().getAttribute("forget") == "1"){
                                amountMap.put("forget1", 1);
                            }
                            else if(request.getSession().getAttribute("forget") == "3"){
                                amountMap.put("forget1", 3);
                            }
                            else {
                                amountMap.put("forget1", 2);
                            }
                            result.setData(amountMap);
                            //这里举个例子，如果觉得setData穿的信息不够，还可以用setMessage方法多传一个字符串过去
                            result.setMessage("forget");

                        } catch (Exception e) {
                        }
                        return result;
                    }
                    else if(code.deCode(user1.getPassword()).equals(Password)){
                        session.setAttribute("forget","2");
                        session.setAttribute("login", "1");
                        session.setAttribute("flag", "1");
                        JsonResult<Map<String, Integer>> result = new JsonResult<>();
                        try {
                            Map<String, Integer> amountMap = new HashMap<>();
                            if (request.getSession().getAttribute("login") == "0") {
                                amountMap.put("login1", 0);
                            } else {
                                amountMap.put("login1", 1);
                            }
                            if (request.getSession().getAttribute("flag") == "0") {
                                amountMap.put("flag1", 0);
                            } else {
                                amountMap.put("flag1", 1);
                            }
                            if (request.getSession().getAttribute("forget") == "0") {
                                amountMap.put("forget1", 0);
                            } else if(request.getSession().getAttribute("forget") == "1"){
                                amountMap.put("forget1", 1);
                            }
                            else if(request.getSession().getAttribute("forget") == "3"){
                                amountMap.put("forget1", 3);
                            }
                            else {
                                amountMap.put("forget1", 2);
                            }
                            result.setData(amountMap);
                            //这里举个例子，如果觉得setData穿的信息不够，还可以用setMessage方法多传一个字符串过去
                            result.setMessage("forget");

                        } catch (Exception e) {
                        }
                        return result;
                    }
                    else{
                        User user = new User();
                        user.setId(user1.getId());
                        enCodeByBase64 encode=new enCodeByBase64();
                        Password = encode.enCode(Password);
                        user.setPassword(Password);
                        user.setUsername(Username);
                        user.setEmail(Email);
                        userRepository.save(user);
                        session.setAttribute("forget","1");
                        session.setAttribute("login", "1");
                        session.setAttribute("flag", "1");
                        JsonResult<Map<String, Integer>> result = new JsonResult<>();
                        try {
                            Map<String, Integer> amountMap = new HashMap<>();
                            if (request.getSession().getAttribute("login") == "0") {
                                amountMap.put("login1", 0);
                            } else {
                                amountMap.put("login1", 1);
                            }
                            if (request.getSession().getAttribute("flag") == "0") {
                                amountMap.put("flag1", 0);
                            } else {
                                amountMap.put("flag1", 1);
                            }
                            if (request.getSession().getAttribute("forget") == "0") {
                                amountMap.put("forget1", 0);
                            }  else if(request.getSession().getAttribute("forget") == "1"){
                                amountMap.put("forget1", 1);
                            }
                            else if(request.getSession().getAttribute("forget") == "3"){
                                amountMap.put("forget1", 3);
                            }
                            else {
                                amountMap.put("forget1", 2);
                            }
                            result.setData(amountMap);
                            //这里举个例子，如果觉得setData穿的信息不够，还可以用setMessage方法多传一个字符串过去
                            result.setMessage("forget");

                        } catch (Exception e) {
                        }
                        return result;
                    }
                }
            }
        }
        session.setAttribute("forget","0");
        session.setAttribute("login", "1");
        session.setAttribute("flag", "1");
        JsonResult<Map<String, Integer>> result = new JsonResult<>();
        try {
            Map<String, Integer> amountMap = new HashMap<>();
            if (request.getSession().getAttribute("login") == "0") {
                amountMap.put("login1", 0);
            } else {
                amountMap.put("login1", 1);
            }
            if (request.getSession().getAttribute("flag") == "0") {
                amountMap.put("flag1", 0);
            } else {
                amountMap.put("flag1", 1);
            }
            if (request.getSession().getAttribute("forget") == "0") {
                amountMap.put("forget1", 0);
            }  else if(request.getSession().getAttribute("forget") == "1"){
                amountMap.put("forget1", 1);
            }
            else if(request.getSession().getAttribute("forget") == "3"){
                amountMap.put("forget1", 3);
            }
            else {
                amountMap.put("forget1", 2);
            }
            result.setData(amountMap);
            //这里举个例子，如果觉得setData穿的信息不够，还可以用setMessage方法多传一个字符串过去
            result.setMessage("forget");

        } catch (Exception e) {
        }
        return result;
    }
    @GetMapping("/Forget.html")
    public String forget(HttpSession session) {
        session.setAttribute("forget", "1");
        return "Forget";
    }
}

